﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Agent
{
    [Serializable]
    public class NewCareModel
    {
        public int num { get; set; }
        public bool selected { get; set; }
        public int skuId { get; set; }
    }
    public class CartJson
    {
        public const string CookieNameStr = "cart";
        public const string CookieCartKey = "cart";
        public const string CookieDomain = "";//".zuihaola.com";
        /// <summary>
        /// 写入cookie
        /// </summary>
        /// <param name="CartList"></param>
        public static void CookieWrite(List<string> CartList)
        {
            HttpCookie cookie = new HttpCookie(CartJson.CookieNameStr);

            cookie.Expires = DateTime.Now.AddYears(-50);

            System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
            System.Web.HttpContext.Current.Request.Cookies.Remove(CartJson.CookieNameStr);


            System.Web.HttpContext.Current.Response.Cookies.Clear();

            HttpCookie shopcar = new HttpCookie(CookieNameStr);
            shopcar.Values.Clear();
            shopcar.Value = HttpUtility.UrlEncode(GetJsonFormatList(CartList));
            shopcar.Expires = DateTime.Now.AddDays(7);
            System.Web.HttpContext.Current.Response.Cookies.Add(shopcar);

        }

        public static List<string> CookieRead()
        {

            if (System.Web.HttpContext.Current.Request.Cookies[CookieNameStr] == null)
            {

                return new List<string>();

            }
            else
            {

                return GetListFormatJson(HttpUtility.UrlDecode(System.Web.HttpContext.Current.Request.Cookies[CookieNameStr].Value.ToString()));

            }

        }

        /// <summary>
        /// 序列化对象集合
        /// </summary>
        /// <param name="List"></param>
        /// <returns></returns>
        public static string GetJsonFormatList(List<string> List)
        {
            System.Web.Script.Serialization.JavaScriptSerializer Json = new System.Web.Script.Serialization.JavaScriptSerializer();
            return Json.Serialize(List);
        }
        /// <summary>
        /// cookie是否存在
        /// </summary>
        /// <returns></returns>
        public static bool ExistCookie()
        {
            if (System.Web.HttpContext.Current.Request.Cookies[CookieNameStr] == null)
                return false;
            return true;
        }
        /// <summary>
        /// 反序列化成一个对象
        /// </summary>
        /// <param name="Json"></param>
        /// <returns></returns>
        public static List<string> GetListFormatJson(string Json)
        {
            if (Json != "")
            {
                System.Web.Script.Serialization.JavaScriptSerializer Js = new System.Web.Script.Serialization.JavaScriptSerializer();
                return Js.Deserialize<List<string>>(Json);
            }
            else
            {
                return null;
            }
        }




    }
}